package com.ruoyi.canteen.mapper;

import java.util.List;
import com.ruoyi.canteen.domain.CanteenOrderAddress;
import com.ruoyi.canteen.vo.CanteenOrderAddressVo;
import org.apache.ibatis.annotations.*;

/**
 * 订餐地址Mapper接口
 * 
 * @author zhangw
 * @date 2021-09-19
 */
public interface CanteenOrderAddressMapper 
{
    /**
     * 查询订餐地址
     * 
     * @param id 订餐地址主键
     * @return 订餐地址
     */
    public CanteenOrderAddress selectCanteenOrderAddressById(Long id);

    /**
     * 查询订餐地址列表
     * 
     * @param canteenOrderAddress 订餐地址
     * @return 订餐地址集合
     */
    public List<CanteenOrderAddress> selectCanteenOrderAddressList(CanteenOrderAddress canteenOrderAddress);

    /**
     * 新增订餐地址
     * 
     * @param canteenOrderAddress 订餐地址
     * @return 结果
     */
    public int insertCanteenOrderAddress(CanteenOrderAddress canteenOrderAddress);

    /**
     * 修改订餐地址
     * 
     * @param canteenOrderAddress 订餐地址
     * @return 结果
     */
    public int updateCanteenOrderAddress(CanteenOrderAddress canteenOrderAddress);

    /**
     * 删除订餐地址
     * 
     * @param id 订餐地址主键
     * @return 结果
     */
    public int deleteCanteenOrderAddressById(Long id);

    /**
     * 批量删除订餐地址
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteCanteenOrderAddressByIds(Long[] ids);

    /**
     * 作废订餐地址
     *
     * @param id 订餐地址主键
     * @param userId 作废操作人ID
     * @return 结果
     */
    @Update("update canteen_order_address set delete_time=NOW() where id=#{id} and user_id=#{userId}")
    public int invalidCanteenOrderAddressById(@Param("id") Long id, @Param("userId") Long userId);

    /**
     * 设置默认订餐地址
     * @param id 订餐地址主键
     * @param userId 作废操作人ID
     * @return 结果
     */
    @Update("update canteen_order_address set is_default=1 where id=#{id} and user_id=#{userId};" +
            "update canteen_order_address set is_default=0 where id<>#{id} and user_id=#{userId};")
    public int setDefaultCanteenOrderAddress(@Param("id") Long id, @Param("userId") Long userId);

    /**
     * 查询订餐地址列表
     *
     * @param userId 用户id
     * @return 订餐地址集合
     */
    @Select("<script>" +
            "SELECT id,user_id,link_man,phone,address,is_default,dict_value as buildingAddress,IFNULL(dict_label, '') as buildingAddressText from canteen_order_address as a " +
            "left join (SELECT dict_label,dict_value from sys_dict_data where dict_type='location_building' and `status`=0) as d " +
            "on a.building_address = d.dict_value " +
            "where delete_time is  null and user_id = #{userId} " +
            "<if test=\'isDefault != null\'> and is_default = #{isDefault} </if> "+
            "</script>"
    )
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "userId",column = "user_id"),
            @Result(property = "linkMan",column = "link_man"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "address",column = "address"),
            @Result(property = "buildingAddressText",column = "buildingAddressText"),
            @Result(property = "buildingAddress",column = "buildingAddress"),
            @Result(property = "isDefault",column = "is_default"),
    })
    public List<CanteenOrderAddressVo> selectCanteenOrderAddressListByUserId(
            @Param("userId") Long userId, @Param("isDefault")  Integer isDefault);

}
